package com.common.base.utils

object NullEx {

    /**
     * 判断1个变量都非空执行block, 参数a, b为对象的非null类型
     *  val result = allNotNullElse(first) { a, b, c, d ->
     *     "$a, $b, $c is all not null"
     *  } ({ "one of them is null"})
     */
    fun <A, R> notNullElse(first: A?, block: (A) -> R) : (() -> R) -> R {
        return {
            if (first != null) {
                block(first)
            } else {
                it()
            }
        }
    }

    /**
     * 判断2个变量都非空执行block, 参数a, b为对象的非null类型
     * allNotNull(first, second) { a, b ->
     *     printText("$a, $b is all not null")
     * }
     */
    fun <A, B> allNotNull(first: A?, second: B?, block: (A, B) -> Unit) {
        if (first != null && second != null) block(first, second)
    }

    /**
     * 判断3个变量都非空执行block, 参数a, b为对象的非null类型
     * allNotNull(first, second) { a, b ->
     *     printText("$a, $b is all not null")
     * }
     */
    fun <A, B, C> allNotNull(first: A?, second: B?, third: C?, block: (A, B, C) -> Unit) {
        if (first != null && second != null && third != null) block(first, second, third)
    }

    /**
     * 判断3个变量都非空执行第一个block, 参数a, b, c为对象的非null类型；为null则执行第二个block
     *  val result = allNotNullElse(first, second, third) { a, b, c ->
     *     "$a, $b, $c is all not null"
     *  } ({ "one of them is null"})
     */
    fun <A, B, C, R> allNotNullElse(first: A?, second: B?, third: C?, block: (A, B, C) -> R) : (() -> R) -> R {
        return {
            if (first != null && second != null && third != null) {
                block(first, second, third)
            } else {
                it()
            }
        }
    }

    /**
     * 判断4个变量都非空执行block, 参数a, b为对象的非null类型
     * allNotNull(first, second, third, fourth) { a, b, c, d ->
     *     printText("$a, $b is all not null")
     * }
     */
    fun <A, B, C, D> allNotNull(first: A?, second: B?, third: C?, fourth: D?, block: (A, B, C, D) -> Unit) {
        if (first != null && second != null && third != null && fourth != null) block(first, second, third, fourth)
    }

    fun <A, B, C, D, E> allNotNull(first: A?, second: B?, third: C?, fourth: D?, fifth: E?, block: (A, B, C, D, E) -> Unit) {
        if (first != null && second != null && third != null && fourth != null && fifth != null) block(first, second, third, fourth, fifth)
    }

    /**
     * 判断4个变量都非空执行block, 参数a, b为对象的非null类型
     *  val result = allNotNullElse(first, second, third, fourth) { a, b, c, d ->
     *     "$a, $b, $c is all not null"
     *  } ({ "one of them is null"})
     */
    fun <A, B, C, D, R> allNotNullElse(first: A?, second: B?, third: C?, fourth: D?, block: (A, B, C, D) -> R) : (() -> R) -> R {
        return {
            if (first != null && second != null && third != null && fourth != null) {
                block(first, second, third, fourth)
            } else {
                it()
            }
        }
    }

}